<?php

namespace app\model;

use think\Model;
use think\model\relation\HasOne;

class MerchantBalanceNotice extends Model
{
    public function user(): HasOne
    {
        return $this->hasOne(Merchant::class, "id", "mid");
    }

    public function pageData(): array
    {
        $param = request()->param();
        $data = $this->with(['user'])->when(!empty($param['create_time']), function ($query) use ($param) {
            $query->where('create_time', 'between', [$param['create_time'][0] . " 00:00:00", $param['create_time'][1] . " 23:59:59"]);
        })->when(!empty($param['username']), function ($query) use ($param) {
            $mid = Merchant::where('name', 'like', "%{$param['username']}%")->column('id');
            $query->whereIn('mid', $mid);
        })->when(!empty($param['type']), function ($query) use ($param) {
            switch ((int)$param['type']) {
                case 2:
                    $query->where('type', 1)->where('tag', 2);
                    break;
                case 3:
                    $query->where('type', 2);
                    break;
                case 4:
                    $query->where('type', 4)->where('tag', 2);
                    break;
                case 5:
                    $query->where('type', 3)->where('tag', 1);
                    break;
                default:
                    $query->where('type', 1)->where('tag', 1);
                    break;
            }
        })->order(['create_time' => 'desc'])->paginate(['list_rows' => 30, 'page' => $param['page'] ?? 1])->toArray();
        return pageData($data);
    }
}
